package team.knowers.qzhserver.course.datum.datum.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import team.knowers.qzhserver.common.api.CommonResult;
import team.knowers.qzhserver.course.course.entity.dao.CourseDO;
import team.knowers.qzhserver.course.course.service.CourseService;
import team.knowers.qzhserver.course.datum.datum.entity.dao.DatumDO;
import team.knowers.qzhserver.course.datum.datum.entity.dto.DatumUpdateDTO;
import team.knowers.qzhserver.course.datum.datum.service.DatumService;
import team.knowers.qzhserver.user.user.service.UserService;

import java.util.Objects;

/**
 * @author zcb
 * @date 2024/3/6 14:00
 */
@Controller
@Tag(name = "DatumController", description = "课程资料库操作")
@RequestMapping("/datum")
public class DatumController {
    @Autowired
    private DatumService datumService;
    @Autowired
    private UserService userService;
    @Autowired
    private CourseService courseService;
    @Operation(summary = "创建资料库")
    @PostMapping("/create")
    @ResponseBody
    public CommonResult createDatum(@RequestParam String cid,@RequestParam String uid,@RequestParam String name){
        //防止参数不合理
        if(userService.userStatus(uid)==-1)
            return CommonResult.failed("用户不存在");
        if(courseService.getCourseById(cid) == null)
            return CommonResult.failed("课程不存在");
        if(name == null || name.isEmpty())
            return CommonResult.failed("资料库名称不能为空");
        DatumDO datumDO = datumService.createDatum(cid,uid,name);
        if(datumDO!=null){
            return CommonResult.success(datumDO,"成功创建资料库");
        }else{
            return CommonResult.failed("创建资料库失败");
        }
    }
    @Operation(summary = "更新资料库信息")
    @PutMapping("/update")
    @ResponseBody
    public CommonResult updateDatum(@RequestBody DatumUpdateDTO datumUpdateDTO){
        //防止参数不合理
        if(userService.userStatus(datumUpdateDTO.getUid())==-1)
            return CommonResult.failed("用户不存在");
        DatumDO datumDO = datumService.getDatumById(datumUpdateDTO.getId());
        if(datumDO==null)
            return CommonResult.failed("资料库不存在");
        if(!Objects.equals(datumDO.getUserId(), datumUpdateDTO.getUid()))
            return CommonResult.failed("该资料库不属于该用户");
        if(datumService.updateDatum(datumUpdateDTO)){
            return CommonResult.success(null,"更新资料库信息成功");
        }else {
            return CommonResult.failed("更新资料库信息失败");
        }
    }
    @Operation(summary = "删除资料库")
    @DeleteMapping("/delete")
    @ResponseBody
    public CommonResult deleteDatum(@RequestParam String id,@RequestParam String uid) throws Exception {
        //防止参数不合理
        if(userService.userStatus(uid)==-1)
            return CommonResult.failed("用户不存在");
        DatumDO datumDO = datumService.getDatumById(id);
        if(datumDO==null)
            return CommonResult.failed("资料库不存在");
        if(!Objects.equals(datumDO.getUserId(), uid))
            return CommonResult.failed("该资料库不属于该用户");
        if(datumService.deleteDatum(id)){
            return CommonResult.success(null,"删除资料库成功");
        }else {
            return CommonResult.failed("删除资料库失败");
        }
    }
}
